package servlet;

import com.alibaba.fastjson2.JSON;
import dto.ResultDto;
import entity.PowerBattery;
import service.PowerBatteryService;
import service.impl.PowerBatteryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        PowerBatteryService powerBatteryService=new PowerBatteryServiceImpl();
        List<PowerBattery> allPowerBattery = powerBatteryService.getAllPowerBattery();
        ResultDto resultDto=new ResultDto();
        resultDto.setStatus(1);
        resultDto.setMessage("获取数据成功！");
        resultDto.setData(allPowerBattery);

        out.print(JSON.toJSONString(resultDto));
        out.flush();
        out.close();
    }
}
